package com.ac.input.dm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.caac.core.dm.BaseDm;
import com.caac.core.util.ListMapClass;
import com.ac.input.config.OrderClass;
import com.ac.input.config.QueryClass;
import com.ac.input.config.ViewClass;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright 2004</p>
 * <p>Company: </p>
 * @author <a href="mailto:lianzs@agilecentury.com">lianzs</a>
 * @version 1.0
 */

public class HndInpDef extends BaseDm {
    private String inpCode;
    private String inpName;
    private String inpDesc;
    private String schSql;
    private String tabName;
    private String uptFlag;
    private String cfmFlag;
    private String keyType;
    private String schUrl;
    private String edtUrl;
    private String addUrl;
    private String keyCode;

	private String insertSql;
    private String updateSql;

    private List cols;
    private ListMapClass mapCols = new ListMapClass();;
    private ViewClass viewClass;
    private OrderClass orderClass;
    private QueryClass queryClass;

    public String getKeyCode() {
		return keyCode;
	}

	public void setKeyCode(String keyCode) {
		this.keyCode = keyCode;
	}

	public String getInpCode() {
        return this.inpCode;
    }

    public void setInpCode(String inpCode) {
        this.inpCode = inpCode;
    }

    public String getInpName() {
        return this.inpName;
    }

    public void setInpName(String inpName) {
        this.inpName = inpName;
    }

    public String getInpDesc() {
        return this.inpDesc;
    }

    public void setInpDesc(String inpDesc) {
        this.inpDesc = inpDesc;
    }

    public String getSchSql() {
        return this.schSql;
    }

    public void setSchSql(String schSql) {
        this.schSql = schSql;
    }

    public String getTabName() {
        return this.tabName;
    }

    public void setTabName(String tabName) {
        this.tabName = tabName;
    }

    public String getUptFlag() {
        return this.uptFlag;
    }

    public void setUptFlag(String uptFlag) {
        this.uptFlag = uptFlag;
    }

    public String getCfmFlag() {
        return this.cfmFlag;
    }

    public void setCfmFlag(String cfmFlag) {
        this.cfmFlag = cfmFlag;
    }

    public String getKeyType() {
        return this.keyType;
    }

    public void setKeyType(String keyType) {
        this.keyType = keyType;
    }

    public String getSchUrl() {
        return this.schUrl;
    }

    public void setSchUrl(String schUrl) {
        this.schUrl = schUrl;
    }

    public String getEdtUrl() {
        return this.edtUrl;
    }

    public void setEdtUrl(String edtUrl) {
        this.edtUrl = edtUrl;
    }

    public String getAddUrl() {
        return this.addUrl;
    }

    public List getCols() {
        return cols;
    }

    public OrderClass getOrderClass() {
        return orderClass;
    }

    public QueryClass getQueryClass() {
        return queryClass;
    }

    public ViewClass getViewClass() {
        return viewClass;
    }

    public void setAddUrl(String addUrl) {
        this.addUrl = addUrl;
    }

    public void setCols(List cols) {
        this.cols = cols;
        viewClass = new ViewClass(cols);
        orderClass = new OrderClass(cols);
        queryClass = new QueryClass(cols);
        String sql1 = "Insert into " + this.tabName;
        String strField1 = "(";
        String strValue1 = " Values(";

        String sql2 = "Update " + this.tabName +" Set ";
        String strField2 = "";
        //String strWhere2 = " Where";
        
        for (int i = 0; i < cols.size(); i++) {
            HndInpDefItm dm = (HndInpDefItm) cols.get(i);
            mapCols.addChild(dm.getColCode(), dm);
            if(dm.isKey()){
            //	strWhere2 += dm.getColCode()+"="+"?,";
            	keyCode = dm.getColCode();
            }
            if(!"0".equals(dm.getIsInput())){
	            strField1 += dm.getColCode()+",";
	            strValue1 += "?,";
	            strField2 += dm.getColCode()+"="+"?,";
            }
        }
        
        strField1 = strField1.substring(0, strField1.length() - 1) + ")";
        strValue1 = strValue1.substring(0, strValue1.length() - 1) + ")";
        insertSql = sql1 + strField1 + strValue1;
        
        strField2 = strField2.substring(0, strField2.length() - 1);
        //strWhere2 = strWhere2.substring(0, strWhere2.length() - 1);
        updateSql = sql2 + strField2; //+ strWhere2;   
    }

    public void setOrderClass(OrderClass orderClass) {
        this.orderClass = orderClass;
    }

    public void setQueryClass(QueryClass queryClass) {
        this.queryClass = queryClass;
    }

    public void setViewClass(ViewClass viewClass) {
        this.viewClass = viewClass;
    }

    protected int doHashCode() {
        return getHashCodeBuilder().append(inpCode).toHashCode();
    }

    protected boolean doEquals(Object object) {
        HndInpDef other = (HndInpDef) object;
        return getEqualsBuilder().append(inpCode, other.inpCode).isEquals();
    }

	public ListMapClass getMapCols() {
		return mapCols;
	}
	
	public HndInpDefItm getColByKey(String colCode) {
		return (HndInpDefItm)mapCols.getChild(colCode);
	}

	public String getInsertSql() {
		return insertSql;
	}

	public void setInsertSql(String insertSql) {
		this.insertSql = insertSql;
	}

	public String getUpdateSql() {
		return updateSql;
	}

	public void setUpdateSql(String updateSql) {
		this.updateSql = updateSql;
	}
}
